<?php
/**
 * 生成二维码.
 * User: liu
 * Date: 2017/3/14
 * Time: 16:21
 */
namespace app\mobile\controller;

use think\Loader;
use think\Request;
use think\Session;
use think\Cache;
use think\Db;

class ScanCode extends BaseController
{
    /**
     * 生成带logo的二维码图片
     * @param int $uid 会员id
     * @param int $level 二维码容错级别
     * @param int $size 需要生成的图片大小
     */
    public function qrcodeWithPicture($uid)
    {
        $level = 7;
        $size = 8;
        $url = Request::instance()->param('url');
        Loader::import('phpqrcode.phpqrcode',EXTEND_PATH);
        //$url = Request::instance()->domain()."/index.php/mobile/user_port/login?uid=".$uid;
        // $url = "http://www.hillyzx.com/index.php/mobile/user_port/login?uid=".$uid;
        //容错级别
        $errorCorrectionLevel = intval($level);
        //生成图片大小
        $matrixPointSize = intval($size);
        //生成二维码图片
        $object = new \QRcode();
        $path = ROOT_PATH . 'public/static/mobile/img_code/qrcode_'.$uid.'.png';

        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
        $object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);
        //准备好的logo图片
        $logo = Request::instance()->domain()."/static/mobile/img_user/qrcode_logo.png";
        //已经生成的原始二维码图
        $qrcode = $path;

        //logo图片存在
        if ($logo !== FALSE) {
            $qrcode = imagecreatefromstring(file_get_contents($qrcode));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $qrcode_width = imagesx($qrcode);   //二维码图片宽度
            $qrcode_height = imagesy($qrcode);  //二维码图片高度
            $logo_width = imagesx($logo);       //logo图片宽度
            $logo_height = imagesy($logo);      //logo图片高度
            $logo_qr_width = $qrcode_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($qrcode_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小[生成中间带logo的二维码  $qrcode]
            imagecopyresampled($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

        }
        //2.输出二维码图片
        imagepng($qrcode, $path);
        //背景图片
        $bgImg = ROOT_PATH . "public/static/mobile/img_user/wap_share.jpg";
        $bgImg = imagecreatefromstring(file_get_contents($bgImg));
        $bgWidth = imagesx($bgImg);       //背景图片宽度
        $bgHight = imagesy($bgImg);      //背景图片高度
        $code_width = imagesx($qrcode)*0.85;       //二维码图片宽度
        $code_height = imagesy($qrcode)*0.85;      //二维码图片高度
        $bg_from_width = ($bgWidth - $code_width) / 2;
        //重新组合图片并调整大小[二维码和背景图片组合  $bgImg]
        imagecopyresampled($bgImg, $qrcode, $bg_from_width*1.015, $bg_from_width*3.685, 0, 0, $code_width, $code_height, imagesx($qrcode), imagesy($qrcode));
        //三合一图片
        imagepng($bgImg, $path);
    }

    public function erweima(){

        //androi https://www.pgyer.com/YfKo;
        $type =Request::instance()->param('type');
        $url = '';
        if($type == 'ios') $url = '/ios.png?a='.rand(0,999);
        if($type == 'android') $url = '/android.png?a='.rand(0,999);
        return $this->fetch('',['url' => $url]);
    }

    public function qrcodeWithPicture_self($uid,$url)

    {
        $level = 7;
        $size = 100;
        //$url = Request::instance()->param('uid');
        Loader::import('phpqrcode.phpqrcode',EXTEND_PATH);
        //$url = 'https://www.baidu.com/?tn=98012088_10_dg&ch=3';

        // $url = "http://app65.app.longcai.net/index.php/mobile/user_port/login?uid=".$uid;

        //容错级别

        $errorCorrectionLevel = intval($level);

        //生成图片大小

        $matrixPointSize = intval($size);

        //生成二维码图片

        $object = new \QRcode();

        $path = ROOT_PATH . 'public/static/mobile/self/'.$uid.'.png';


        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件

        $ret = $object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);
//        //准备好的logo图片
//
//        $logo = Request::instance()->domain()."/static/mobile/img_user/qrcode_logo.jpg";
//
//        //已经生成的原始二维码图
//
//        $qrcode = $path;
//
//
//
//        //logo图片存在
//
//        if ($logo !== FALSE) {
//
//            $qrcode = imagecreatefromstring(file_get_contents($qrcode));
//
//            $logo = imagecreatefromstring(file_get_contents($logo));
//
//            $qrcode_width = imagesx($qrcode);   //二维码图片宽度
//
//            $qrcode_height = imagesy($qrcode);  //二维码图片高度
//
//            $logo_width = imagesx($logo);       //logo图片宽度
//
//            $logo_height = imagesy($logo);      //logo图片高度
//
//            $logo_qr_width = $qrcode_width / 5;
//
//            $scale = $logo_width / $logo_qr_width;
//
//            $logo_qr_height = $logo_height / $scale;
//
//            $from_width = ($qrcode_width - $logo_qr_width) / 2;
//
//            //重新组合图片并调整大小[生成中间带logo的二维码  $qrcode]
//
//            imagecopyresampled($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
//
//
//
//        }
//
//        //2.输出二维码图片
//
//        imagepng($qrcode, $path);
//
//        //背景图片
//
//        $bgImg = ROOT_PATH . "public/static/mobile/img_user/wap_share.png";
//
//        $bgImg = imagecreatefromstring(file_get_contents($bgImg));
//
//        $bgWidth = imagesx($bgImg);       //背景图片宽度
//
//        $bgHight = imagesy($bgImg);      //背景图片高度
//
//        $code_width = imagesx($qrcode)*0.85;       //二维码图片宽度
//
//        $code_height = imagesy($qrcode)*0.85;      //二维码图片高度
//
//        $bg_from_width = ($bgWidth - $code_width) / 2;
//
//        //重新组合图片并调整大小[二维码和背景图片组合  $bgImg]
//
//        imagecopyresampled($bgImg, $qrcode, $bg_from_width*1.015, $bg_from_width*3.685, 0, 0, $code_width, $code_height, imagesx($qrcode), imagesy($qrcode));
//
//        //三合一图片
//
//        imagepng($bgImg, $path);

    }



    /*

     * 展示用户二维码

     */

    public function qrcode_self()
    {
        $uid = Request::instance()->param('uid',0);
        $url = Request::instance()->param('url',0);

        if(Request::instance()->isGet()){
            self::qrcodeWithPicture_self($uid,$url);
        }

        $member_list = Db::name('member')->select();

        //分享sdk参数

        // $sign = (new WxConfig())->sdk_config(Request::instance()->url(true));

        return $this->fetch('scan_code_qrcode_self',[
            'img'       =>  '/static/mobile/self/'.$uid.'.png?a=' . rand(0,1000),
            'member_list'  => $member_list,
            'user_id'   =>  $uid,

        ]);

    }

    /*
     * 展示用户二维码
     */
    public function qrcode()
    {
        $uid = Request::instance()->param('uid');
        $flag = !Request::instance()->has('flag','get')?'':Request::instance()->get('flag');

        if (!file_exists(ROOT_PATH.'/static/mobile/img_code/qrcode_'.$uid.'.png')){  //如果文件不存在则生成一个
            self::qrcodeWithPicture($uid);
        }
        //分享sdk参数
         $sign = (new WxConfig())->sdk_config(Request::instance()->url(true));
        return $this->fetch('qrcode',[
            'img'       =>      '/static/mobile/img_code/qrcode_'.$uid.'.png',
            'signPackage'  =>  $sign,
            'user_id'   =>  $uid,
            'flag'      =>  $flag,
        ]);
    }

    public function qrcodeWithPicture_xiao($uid = '', $level = 7, $size = 8)

    {

        Loader::import('phpqrcode.phpqrcode',EXTEND_PATH);

        $url = Request::instance()->domain()."/index.php/mobile/user_port/login?uid=".$uid;

        // $url = "http://www.hillyzx.com/index.php/mobile/user_port/login?uid=".$uid;

        //容错级别

        $errorCorrectionLevel = intval($level);

        //生成图片大小

        $matrixPointSize = intval($size);

        //生成二维码图片

        $object = new \QRcode();

        $path = ROOT_PATH . 'public/static/mobile/img_code/qrcodex_'.$uid.'.png';



        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件

        $object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);

        //准备好的logo图片

//        $logo = Request::instance()->domain()."/static/mobile/img_user/qrcode_logo.jpg";

        //已经生成的原始二维码图

        $qrcode = $path;



        //logo图片存在
//
//        if ($logo !== FALSE) {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfad7142a43ba4835&secret=9ee49a690ce634505474649472047c9a";
        $access_token=get_object_vars(json_decode(file_get_contents($url)));
//            https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
        $url1  = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token['access_token']."";
//            $post['action_name'] = 'QR_LIMIT_SCENE';
//            $post['action_info'] = ['scene'=>['scene_str'=>"444"]];
        $post = json_encode(['scene'=>$uid]);
//            echo $post;
//            echo $url1;
//            $ceshi=curl_post1($url1,$post);
//            echo $ceshi;
//            $post = '{"action_name" "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}';
        $qrcode=curl_post1($url1,$post);
        $time  = time()+rand(0,10000);
        $url2='./uploads/xiaochengxu/'.$time.'.jpg';
        file_put_contents($url2,$qrcode);

        $qrcode = imagecreatefromstring(file_get_contents($url2));
//            $logo = imagecreatefromstring(file_get_contents($logo));

//            $qrcode_width = imagesx($qrcode);   //二维码图片宽度
//
//            $qrcode_height = imagesy($qrcode);  //二维码图片高度
//
////            $logo_width = imagesx($logo);       //logo图片宽度
////
////            $logo_height = imagesy($logo);      //logo图片高度
//
//            $logo_qr_width = $qrcode_width / 5;
//
//            $scale = $logo_width / $logo_qr_width;
//
//            $logo_qr_height = $logo_height / $scale;
//
//            $from_width = ($qrcode_width - $logo_qr_width) / 2;
//
//            //重新组合图片并调整大小[生成中间带logo的二维码  $qrcode]
//
//            imagecopyresampled($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);



//        }

        //2.输出二维码图片

        imagepng($qrcode, $path);

        //背景图片

        $bgImg = ROOT_PATH . "public/static/mobile/img_user/wap_share.jpg";

        $bgImg = imagecreatefromstring(file_get_contents($bgImg));

        $bgWidth = imagesx($bgImg);       //背景图片宽度

        $bgHight = imagesy($bgImg);      //背景图片高度

        $code_width = imagesx($qrcode)*0.85;       //二维码图片宽度

        $code_height = imagesy($qrcode)*0.85;      //二维码图片高度

        $bg_from_width = ($bgWidth - $code_width) / 2;

        //重新组合图片并调整大小[二维码和背景图片组合  $bgImg]

        imagecopyresampled($bgImg, $qrcode, $bg_from_width*1.015, $bg_from_width*4.585, 0, 0, $code_width, $code_height, imagesx($qrcode), imagesy($qrcode));

        //三合一图片

        imagepng($bgImg, $path);

    }

    public function qrcode_xiao()

    {

        $uid = Request::instance()->param('uid');

        $flag = !Request::instance()->has('flag','get')?'':Request::instance()->get('flag');



        if (!file_exists(ROOT_PATH.'/static/mobile/img_code/qrcodex_'.$uid.'.png')){  //如果文件不存在则生成一个

            self::qrcodeWithPicture_xiao($uid);

        }

        //分享sdk参数

         $sign = (new WxConfig())->sdk_config(Request::instance()->url(true));

        return $this->fetch('qrcode',[

            'img'       =>      '/static/mobile/img_code/qrcodex_'.$uid.'.png',

            'signPackage'  =>  $sign,

            'user_id'   =>  $uid,

            'flag'      =>  $flag,

        ]);

    }

}